from django.db import models
from django.utils import timezone
from epharma.db.models import AbstractBaseModel


class NationalInsuranceHouseArticle(AbstractBaseModel):
    """An Article strictly defined and controlled by the National Health Insurance House in Romania.
    These Articles are usually prescription drugs and off-the-counter-drugs."""
    name = models.CharField(max_length=100)


class MinistryOfHealthArticle(AbstractBaseModel):
    """An Article strictly defined and controlled by the Ministry of Health in Romania.
    These Articles are usually prescription drugs and off-the-counter drugs."""
    name = models.CharField(max_length=100)


class ArticleBrand(AbstractBaseModel):
    """A brand name given to a group of Articles made by the same Producer."""
    name = models.CharField(max_length=100)


class ArticleCategory(AbstractBaseModel):
    """A specific name used to group similar Articles together."""
    name = models.CharField(max_length=100)


class FreeAndCompensatedArticlesList(AbstractBaseModel):
    valid_from = models.DateTimeField(default=timezone.now())
    name = models.CharField(max_length=100, blank=True,
                            default="Lista de medicamente gratuite si compensate valabila din " +
                                    timezone.now().strftime("%d-%b-%Y"))


class ArticleCompensation(AbstractBaseModel):
    article = models.ForeignKey(NationalInsuranceHouseArticle)
    list = models.ForeignKey(FreeAndCompensatedArticlesList)
    # TODO Add compensation fields and compensation sublist FKs, etc.


class ActiveSubstanceConcentration(AbstractBaseModel):
    """The active substance concentration in the total mass or volume."""
    name = models.CharField(max_length=100)
    # massvol_of_solute = models.FloatField(null=True, blank=True)
    # massvol_of_solution = models.FloatField(null=True, blank=True)
    # percent_by_massvol = models.FloatField(default=(massvol_of_solute/massvol_of_solution)*100.0, null=True, blank=True)
    # Go to http://chem.wisc.edu/deptfiles/genchem/sstutorial/Text11/Tx113/tx113.html to read more about different ways to measure concentration


class VAT(AbstractBaseModel):
    """Value Added Tax is a consumption tax that is levied on both goods and services."""
    valid_from = models.DateTimeField(default=timezone.now())
    # TODO Implement validator to check that only one VAT is valid in a given time period

    valid_to = models.DateTimeField(null=True, blank=True)
    # TODO Implement validator to let only one valid_to record = null at one time, ie. an undetermined valid_to

    percent = models.FloatField()


class BarcodeType(AbstractBaseModel):
    """Barcode type, such as EAN (like EAN-13 or EAN-8) or UPC (like UPC-A or UPC-E).
    Refer to http://en.wikipedia.org/wiki/Barcode for more details about barcodes."""
    name = models.CharField(max_length=100)
    validate_barcode_before_saving = models.BooleanField(default=True)


class Barcode(AbstractBaseModel):
    """Barcodes are used to quickly identify a product by scanning a unique code printed on the product package."""
    barcode = models.CharField(max_length=24)
    type = models.ForeignKey(BarcodeType)


class Article(AbstractBaseModel):
    """Articles are SKUs (Stock Keeping Units) used to uniquely identify products in stock."""
    name = models.CharField(max_length=100)
    is_active = models.BooleanField(default=True)
    is_substance = models.BooleanField(default=False)
    # Foreign Keys:
    barcode = models.ForeignKey(Barcode, null=True, blank=True)
    national_insurance_house_article = models.ForeignKey(NationalInsuranceHouseArticle, null=True, blank=True)
    ministry_of_health_article = models.ForeignKey(MinistryOfHealthArticle, null=True, blank=True)
    brand = models.ForeignKey(ArticleBrand, null=True, blank=True)
    category = models.ForeignKey(ArticleCategory)
    vat = models.ForeignKey(VAT)


